/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:lmrp-base
 * Create Time: 2016年2月13日 下午10:48:30
 */
package com.lenovo.lmrp.base;

import java.util.Map;
import java.util.Objects;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.lenovo.sdc.framework.annotation.RestService;
import com.lenovo.sdc.framework.rest.RestClientProxy;
import com.lenovo.sdc.framework.util.exception.ErrorCode;
import com.lenovo.sdc.framework.util.exception.TraceableException;

/**
 * 
 * @author wujin
 * @version v1.0
 */
public class GuiceRestClientProxy extends RestClientProxy {
    @Inject
    private Injector injector;
    
    /**
     * @param scanPackage
     * @param remoteURL
     */
    public GuiceRestClientProxy(String scanPackage, String remoteURL) {
        super(scanPackage, remoteURL);
    }

    protected void createLocalInstance(Class<?> clz, Map<Class<?>, Object> cacheMap) {
        if (Objects.isNull(clz.getAnnotation(RestService.class)))
            return;
        
        try {
            Object inst = Objects.isNull(injector) ? clz.newInstance() : injector.getInstance(clz);
            Class<?>[] intfs = clz.getInterfaces();
            if (Objects.isNull(intfs) || intfs.length == 0)
                cacheMap.put(clz, inst);
            else {
                for (Class<?> intf : intfs)
                    cacheMap.put(intf, inst);
            }
        }
        catch (Exception exp) {
            TraceableException.writeLog(ErrorCode.COMM_ERROR, clz.getName(), exp);
        }
    }
}
